yjx.controller('productController', ['$rootScope', '$scope', '$state', '$timeout','$stateParams', '$fetch', 'modalService','$init','wxShareService','ipCookie', function ($rootScope, $scope,  $state, $timeout,$stateParams, $fetch, modalService,$init,wxShareService,ipCookie) {
    var api = '/product/center';
    $scope.classId = $stateParams.classId;
    $scope.dimensionId = $stateParams.dimensionId;
    // $init.title("产品")
    /**排序相关 */
    $scope.priceDesc=false;
    $scope.salesDesc=false;
    $scope.brokerageDesc=false;
    $scope.orderBy=function(type,desc){
        if(desc){
            $scope.sortObjectType = type;
        }else{
            $scope.sortObjectType = '-'+type;
        }
    }

    /**翻页处理 */
    var params = {
        limit: 10,
        offset: 1,
    }
    //$scope.paging = {};
    $scope.pageNum = 1;
    $scope.hideShows = true;
    //$scope._update = false;

    /*初始化获取数据*/
    function getProduct() {
        if($scope.classId){
            params.classId = $scope.classId;
        }
        if($scope.dimensionId){
            params.dimensionId = $scope.dimensionId;
        }
        $fetch.fetch(
            api,
            {
                params: params,
                // cache:false,
            }
        ).then(function (data) {
                $timeout(function () {
                    $scope.paging = data.page;
                    $scope.dataList = data.list;
                    //更新状态、数据加载控制
                    $scope._updated = 'reupdated';
                    $scope.hideShows = false;
                })
            }).catch(function (e) {
                modalService.mainMsgError(e.msg);
            })
    }
    getProduct();

    /*翻页处理*/
    $scope.list = function () {
        params.offset = $scope.pageNum;
        if($scope.classId){
            params.classId = $scope.classId;
        }
        if($scope.dimensionId){
            params.dimensionId = $scope.dimensionId;
        }
        $fetch.fetch(
            api,
            {
                params: params,
                cache:false,
            }
        ).then(function (data) {
                $timeout(function () {
                    $scope.paging = data.page;
                    //$scope.dataList = data.rows;
                    //if($scope.dataList)
                    $scope.dataList = $scope.dataList.concat(data.list);
                    //更新状态、数据加载控制
                    $scope._updated = 'updated';
                    $scope.hideShows = false;
                })
            }).catch(function (e) {
                modalService.mainMsgError(e.msg);
            })
    }
    $scope.temple='<li ng-repeat="item  in (dataList | orderBy:sortObjectType )" ui-sref="productDetail({id:item.id})" ><product-item2></product-item2></li> '
    $scope.templeSelect='<li class="selectLi" ng-repeat="item  in (dataList | orderBy:sortObjectType )" ng-click="$parent.addProduct(item)" ><product-item2></product-item2></li> '
    
    $scope.$on('checkitFalse',function(id){
        for(var i =0;i<$scope.dataList.length;i++){
            if(id == $scope.dataList[i].id){
                $scope.dataList[i].checkit = false;
                break;
            }
        }
    })
    if (ipCookie('token')) {
        wxShareService.onMenuShareTimeline();
        wxShareService.onMenuShareAppMessage();
    }
}])